package com.rd.student.jike.class2.lihongming.Day0807;

import java.io.*;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @Auther:Heinrich
 * @Date:2023/8/7-08-07-16:34
 * @Description:c230807
 * @version:1.0
 */
public class SuperLotto {
    public static void main(String[] args) throws IOException {
        //WriteLotto();
        //WriteDLT(100);
        WriteNum();
    }
    public static void WriteLotto() throws IOException {
        RandomNum(1,"daletou.txt",false);
    }
    public static void WriteDLT(int num) throws IOException {
        //new File("RandomDLT.txt").delete();
        RandomNum(num,"RandomDLT.txt",true);
    }
    public static void RandomNum(int num,String path,boolean concat) throws IOException {

        for (int i = 0; i < num; i++) {
            Set<Integer> set = new HashSet<Integer>(7);
            while (set.size()<5){
                set.add((int) (Math.random()*35+1));
            }
            while (set.size()<7){
                set.add((int) (Math.random()*12+1));
            }
            writer(path,set,concat);
        }
    }

    public static String reader(String path) throws IOException {
        File fileRandom =  new File(path);
        FileInputStream isRandom = new FileInputStream(fileRandom);
        byte[]  pipe = new byte[(int) fileRandom.length()];
        isRandom.read(pipe);
        isRandom.close();
        return new String(pipe);
    }
    public static void writer(String path, Set<Integer> set, boolean concat) throws IOException {
        File file = new File(path);
        FileOutputStream os = new FileOutputStream(file,concat);
        Iterator<Integer> iterator = set.iterator();
        Integer[] arr =new Integer[7];
        int i =0;
        while (iterator.hasNext()){
            arr[i++] = (Integer) iterator.next();
        }
        StringBuilder n = new StringBuilder();
        for (int j = 0; j <arr.length ; j++) {
            if(arr[j]<10){
                n.append("0");
            }
            if(j == 4){
                n.append(arr[j]).append("|");
            }else{
                n.append(arr[j]).append(" ");
            }
        }
        n.append('\n');
        byte[] pipe  = n.toString().getBytes();
        os.write(pipe);
        os.close();
    }
    public static void WriteNum() throws IOException {
        String answer = reader("daletou.txt");
        Set<String> red1 =  new HashSet<>(Arrays.asList(answer.split("\\|")[0].split(" ")));
        Set<String> blue1 = new HashSet<>(Arrays.asList(answer.split("\\|")[1].split(" ")));
        blue1.remove("\n");
        BufferedReader reader = new BufferedReader(new FileReader(new File("RandomDLT.txt")));
        int num = 0;
        while (true){
            String str = reader.readLine();
            if(str==null){
                break;
            }
            Set<String> red = new HashSet<>(Arrays.asList(str.split("\\|")[0].split(" "))) ;
            Set<String> blue = new HashSet<>(Arrays.asList(str.split("\\|")[1].split(" ")));
            red.removeAll(red1);blue.removeAll(blue1);
            if(red.size()==0&&blue.size()==0){
                num++;
            }
        }
        OutputStream os = Files.newOutputStream(new File("Num.txt").toPath());
        byte[] pipe  =String.valueOf(num).getBytes();
        os.write(pipe);
        os.close();
        reader.close();
    }

}
